package com.walnut.lom_system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.walnut.lom_system.common.annotation.DoLog;
import com.walnut.lom_system.common.lang.Const;
import com.walnut.lom_system.common.lang.OperateTypeContains;
import com.walnut.lom_system.common.lang.ResultInfo;
import com.walnut.lom_system.common.util.ResultInfoUtil;
import com.walnut.lom_system.domain.form.EmployInfoForm;
import com.walnut.lom_system.domain.form.EmployInfoSearchForm;
import com.walnut.lom_system.domain.form.UserInfoForm;
import com.walnut.lom_system.domain.form.UserInfoSearchForm;
import com.walnut.lom_system.domain.vo.UserInfoVo;
import com.walnut.lom_system.service.EmployInfoService;
import com.walnut.lom_system.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author chenhetao
 * @version 1.0
 * @className AdminEmployController
 * @description TODO
 * @date 2022/4/17 00:10
 */
@RestController
@Api(tags = "用户管理相关接口")
@RequestMapping("/admin/user")
public class AdminUserController {

    private static final Logger LOG = LoggerFactory.getLogger(HelloWorldController.class);

    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("/{id}")
    @ApiOperation("管理员通过id获取指定用户")
    @DoLog(operateModule = "OPERATE", operateType = OperateTypeContains.OPERATE_VISIT, operateDesc = "")
    public ResultInfo<UserInfoVo> queryUser(@NotNull @PathVariable("id") long id){
        UserInfoVo userInfoVo = userInfoService.getUserById(id);//TODO 调整
        return ResultInfoUtil.buildSuccess(userInfoVo);
    }

    @GetMapping("/list")
    @ApiOperation("管理员查询用户列表")
    @DoLog(operateModule = "OPERATE", operateType = OperateTypeContains.OPERATE_VISIT, operateDesc = "")
    public ResultInfo<Page<UserInfoVo>> queryUserList(@RequestBody @Validated UserInfoSearchForm userInfoSearchForm){
        Page<UserInfoVo> userInfoVoList = userInfoService.getList(userInfoSearchForm);
        return ResultInfoUtil.buildSuccess(userInfoVoList);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("管理员删除用户")
    @DoLog(operateModule = "OPERATE", operateType = OperateTypeContains.OPERATE_DELETE, operateDesc = "")
    public ResultInfo deleteUser(@PathVariable("id")@NotNull long id){
        //TODO 代码
        int i = userInfoService.deleteUser(id);

        if (i == Const.OP_SUCCESS) {
            return ResultInfoUtil.buildSuccess("删除成功");
        } else {
            return ResultInfoUtil.buildError("删除失败");
        }
    }

    @PutMapping("/{id}")
    @ApiOperation("管理员更新用户信息")
    @DoLog(operateModule = "OPERATE", operateType = OperateTypeContains.OPERATE_UPDATE, operateDesc = "")
    public ResultInfo<String> updateUser(@PathVariable("id") long id,@RequestBody @Validated UserInfoForm userInfoForm){
        int i = userInfoService.updateUser(id,userInfoForm);
        return ResultInfoUtil.buildSuccess("success");
    }
}
